﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Flow
{
    public partial class FlowList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string sid = Request.QueryString["sid"];
                if (string.IsNullOrWhiteSpace(sid))
                {
                    txtCurrentUser.Visible = false;
                    txtCurrentUser.Text = CurrentInfo.Instance.CurrentUser.UserName;
                }
                else
                {
                    string temp = "dujl-" + DateTime.Now.ToString("yyyy-MM-dd");
                    if (sid.Equals(temp))
                    {
                        txtCurrentUser.Visible = true;
                        txtCurrentUser.Text = "";
                    }
                }

                BindFlowName();
                InitCreateDate();
                BindGridView();
            }
        }

        private void InitCreateDate()
        {
            DateTime dateTime1 = DateTime.Now;
            DateTime dateTime2 = dateTime1.AddMonths(-1);

            txtCreateDateFrom.Text = dateTime2.ToString("yyyy-MM-dd");
            txtCreateDateTo.Text = dateTime1.ToString("yyyy-MM-dd");
        }

        private void BindFlowName()
        {
            try
            {
                DataTable dataTable = FlowHelper.GetFlowMain();
                ddlFlowName.DataSource = dataTable;
                ddlFlowName.DataTextField = "FlowName";
                ddlFlowName.DataValueField = "FlowName";
                ddlFlowName.DataBind();

                ListItem listItem = new ListItem("全部", "");
                ddlFlowName.Items.Insert(0, listItem);
            }
            catch (Exception ex)
            {
                Helper.Instance.SetException(ex);
            }
        }

        private void BindGridView()
        {

            string sql = @"SELECT * FROM dbo.FlowInstance WHERE FlowInstanceID IN
	                            (SELECT DISTINCT FlowInstanceID FROM dbo.FlowInstanceStep A
	                            INNER JOIN dbo.FlowStepOperator B ON A.StepID = B.StepID AND B.OperatorInfo LIKE '%'+@OperatorInfo+'%')
                            AND FlowName LIKE '%'+@FlowName+'%' AND [Subject] LIKE '%'+@Subject+'%' AND CreateUser LIKE '%'+@CreateUser+'%' AND Completed LIKE '%'+@Completed+'%'
                            AND CreateDate BETWEEN @CreateDateFrom AND @CreateDateTo";

            string operatorInfo = txtCurrentUser.Text.Trim();
            string flowName = ddlFlowName.SelectedValue;
            string subject = txtSubject.Text.Trim();
            string createUser = txtCreateUser.Text.Trim();
            string completed = ddlCompleted.SelectedValue;

            string createDateFrom = string.Format("{0} 00:00:00", txtCreateDateFrom.Text.Trim());
            string createDateTo = string.Format("{0} 23:59:59", txtCreateDateTo.Text.Trim());            

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@OperatorInfo", DbType.String, operatorInfo);
                database.AddInParameter(dbCommand, "@FlowName", DbType.String, flowName);
                database.AddInParameter(dbCommand, "@Subject", DbType.String, subject);
                database.AddInParameter(dbCommand, "@CreateUser", DbType.String, createUser);
                database.AddInParameter(dbCommand, "@Completed", DbType.String, completed);
                database.AddInParameter(dbCommand, "@CreateDateFrom", DbType.String, createDateFrom);
                database.AddInParameter(dbCommand, "@CreateDateTo", DbType.String, createDateTo);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

                gvFlowList.DataSource = dataTable;
                gvFlowList.DataBind();
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gvFlowList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell tableCell_FlowCompleted = e.Row.Cells[4];

                if (tableCell_FlowCompleted.Text.ToLower().Equals("true"))
                {
                    tableCell_FlowCompleted.Text = "已完成";
                    e.Row.BackColor = System.Drawing.Color.LightGreen;
                }
                else
                {
                    tableCell_FlowCompleted.Text = "处理中";            
                }

                Button button = e.Row.Cells[6].FindControl("btnOpen") as Button;

                if (button != null)
                {
                    string flowID = gvFlowList.DataKeys[e.Row.RowIndex]["FlowID"].ToString();
                    string flowInstanceID = gvFlowList.DataKeys[e.Row.RowIndex]["FlowInstanceID"].ToString();
                    string url = string.Format("FlowManager.aspx?flowID={0}&FlowInstanceID={1}", flowID, flowInstanceID);

                    button.Attributes.Add("onclick", string.Format("showPage('{0}');", url));
                }
            }
        }
    }
}